package swa23.provider.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import swa23.provider.impl.TelcoProvider;
//import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
//import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import swa2012.telco.broker.BrokerCallback;
//import swa23.provider.impl.TelcoProvider;
import swa23.provider.interfaces.ClientProvider;
/**
 * SServlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
 
    TelcoProvider telcoprovider;   
    JaxWsServerFactoryBean clientFactoryBean;
    JaxWsServerFactoryBean cbFactoryBean;
    
    StringBuffer logString;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Start
		if(request.getParameter("action").contentEquals("start")) {
			logString = new StringBuffer();          
            telcoprovider = new TelcoProvider(logString);
            
            clientFactoryBean = new JaxWsServerFactoryBean();
            clientFactoryBean.setServiceClass(ClientProvider.class);
            clientFactoryBean.setAddress("http://192.168.0.199:8001/Provider/ProviderService");
            clientFactoryBean.setServiceBean(telcoprovider.getClientProvider());
            clientFactoryBean.create();
            
            cbFactoryBean = new JaxWsServerFactoryBean();
            cbFactoryBean.setServiceClass(BrokerCallback.class);
            cbFactoryBean.setAddress("http://192.168.0.199:8001/Provider/BrokerCallbackService");
            cbFactoryBean.setServiceBean(telcoprovider.getBrokerCallback());
            cbFactoryBean.create();
            
            try {
                    
                    telcoprovider.registerCallback(new URI("http://212.186.67.90:8001/Provider/BrokerCallbackService"));
            } catch (URISyntaxException e) {
                    e.printStackTrace();
            }
            
            PrintWriter out = response.getWriter();
            out.write(logString.toString());
            out.close();
		}

		//Stop
		if(request.getParameter("action").contentEquals("stop")) {
			
			telcoprovider.shutDown(); 
            clientFactoryBean.destroy();
            cbFactoryBean.destroy();
            
            PrintWriter out = response.getWriter();
            out.write(logString.toString());
            out.close();

		}
	}

}
